package com.giCompiler.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import com.giCompiler.bean.VinculoUsuarioProjeto;
import com.giCompiler.util.HibernateUtil;

public class VinculoUsuarioProjetoDao {
	
	public void inserir(VinculoUsuarioProjeto vup) { 
	       
		Session session = HibernateUtil.getSessionFactory().openSession(); 		       
		Transaction transaction = null; 		       
		       
		try { 
		            
			transaction = session.beginTransaction(); 		           
		    session.save(vup); 		           
			transaction.commit(); 
		        
		} catch (HibernateException e) { 
		           
			transaction.rollback(); 			           
			e.printStackTrace(); 
		      
		} finally { 		            
			session.close(); 	        
		} 

	} 
	
	public void excluir(VinculoUsuarioProjeto vup){ 
        
		Session session = HibernateUtil.getSessionFactory().openSession(); 		       
		Transaction transaction = null; 
		        
		try { 
		           
			transaction = session.beginTransaction();
			session.delete(vup); 		 
			transaction.commit(); 
		        
		} catch (HibernateException e) { 
		           
			transaction.rollback(); 		            
			e.printStackTrace(); 
		        
		} finally { 
		            
			session.close(); 
	       
		} 
	}
	
	@SuppressWarnings("unchecked")
	public List<VinculoUsuarioProjeto> pegarVup(int idUsuario, int idProjeto){
		
		Session session = HibernateUtil.getSessionFactory().openSession(); 		      
		Transaction transaction = null; 
		List<VinculoUsuarioProjeto> vups = new ArrayList<VinculoUsuarioProjeto>();
		        
		try { 
		            
			transaction = session.beginTransaction(); 			           
							 			 
		    vups = session.createCriteria(VinculoUsuarioProjeto.class).add(Restrictions.eq("idProjeto", idProjeto)).add(Restrictions.eq("idUsuario", idUsuario)).list();	    

			transaction.commit(); 
		       
		}catch (HibernateException e) { 
		        
			transaction.rollback(); 			           
			e.printStackTrace(); 
		       
		} finally { 		           
			session.close(); 		       
		} 
		
		return vups;
		
	}
	
	public void excluirVupDoProjeto(int idProjeto){
		
		Session session = HibernateUtil.getSessionFactory().openSession(); 		      
		Transaction transaction = null; 
		        
		try { 
		            
			transaction = session.beginTransaction(); 			           
							 			 
			Query query = session.createSQLQuery("DELETE FROM gicompiler.TB_VC_USU_PROJ WHERE ID_PROJETO = :idProjeto");					 			 
			
			query.setInteger("idProjeto", idProjeto);
			query.executeUpdate();

			transaction.commit(); 
		       
		}catch (HibernateException e) { 
		        
			transaction.rollback(); 			           
			e.printStackTrace(); 
		       
		} finally { 		           
			session.close(); 		       
		} 
			
	}
		
	public void excluirVupDoUsuario(int idUsuario){
		
		Session session = HibernateUtil.getSessionFactory().openSession(); 		      
		Transaction transaction = null; 
		        
		try { 
		            
			transaction = session.beginTransaction(); 			           
							 			 
			Query query = session.createSQLQuery("DELETE FROM gicompiler.TB_VC_USU_PROJ WHERE ID_USUARIO = :idUsuario");					 			 
			
			query.setInteger("idUsuario", idUsuario);
			query.executeUpdate();

			transaction.commit(); 
		       
		}catch (HibernateException e) { 
		        
			transaction.rollback(); 			           
			e.printStackTrace(); 
		       
		} finally { 		           
			session.close(); 		       
		} 
			
	}
	
	@SuppressWarnings("unchecked")
	public List<VinculoUsuarioProjeto> pegarProjetosUsuario(int idUsuario){ 
        
		List<VinculoUsuarioProjeto> vups = new ArrayList<VinculoUsuarioProjeto>();
		
		Session session = HibernateUtil.getSessionFactory().openSession(); 		      
		Transaction transaction = null; 
		        
		try { 
		            
			transaction = session.beginTransaction(); 			           
							 			 
			Query query = session.createSQLQuery("SELECT * FROM gicompiler.TB_VC_USU_PROJ WHERE ID_USUARIO = :idUsuario AND PERMISSAO = 5;").addEntity(VinculoUsuarioProjeto.class);					 			 
			
			query.setInteger("idUsuario", idUsuario);
			vups = query.list();
			transaction.commit(); 
		       
		}catch (HibernateException e) { 
		        
			transaction.rollback(); 			           
			e.printStackTrace(); 
		       
		} finally { 		           
			session.close(); 		       
		} 
		
        return vups;
	}
	
	@SuppressWarnings("unchecked")
	public List<VinculoUsuarioProjeto> verificarSeExisteCompartilhado(int idUsuario, int idProjeto){
		
		Session session = HibernateUtil.getSessionFactory().openSession(); 		      
		Transaction transaction = null; 
		List<VinculoUsuarioProjeto> vups = new ArrayList<VinculoUsuarioProjeto>();
		        
		try { 
		            
			transaction = session.beginTransaction(); 			           
							 			 
		    vups = session.createCriteria(VinculoUsuarioProjeto.class).add(Restrictions.eq("idProjeto", idProjeto)).add(Restrictions.eq("idUsuario", idUsuario)).list();	    

			transaction.commit(); 
		       
		}catch (HibernateException e) { 
		        
			transaction.rollback(); 			           
			e.printStackTrace(); 
		       
		} finally { 		           
			session.close(); 		       
		} 
		
		return vups;
		
	}
}
